package com.itheima.controller.backend.security.security;

import com.itheima.Service.CheckGroupService;
import com.itheima.common.constant.MessageConstant;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.common.entity.Result;
import com.itheima.pojo.CheckGroup;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("checkgroup")
@Slf4j
public class CheckGroupController {
    @Autowired
    private CheckGroupService checkGroupService;

    @PostMapping("/add")
    public Result add(@RequestParam List<Integer>checkitemIds,
                      @RequestBody CheckGroup checkGroup) {
        log.info("新增检查组");
        System.out.println(checkitemIds);
        checkGroupService.add(checkitemIds,checkGroup);
        return new Result(true, MessageConstant.ADD_CHECKGROUP_SUCCESS,null);
    }

    @GetMapping("/deleteById")
    public Result deleteById(@RequestParam Integer id) {
        log.info("根据id删除检查组");
          checkGroupService.delete(id);
        return new Result(true, MessageConstant.DELETE_CHECKGROUP_SUCCESS,null);
    }

    @GetMapping("findCheckGroupIdsBySetmealId")
    public Result findCheckGroupIdsBySetmealId(@RequestParam Integer setmealId) {
        log.info("根据套餐id查询检查组id");
        List<Integer>reuslt=checkGroupService.findCheckGroupIdsBySetmealId(setmealId);
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,reuslt);
    }

    @PostMapping("/findPage")
    public PageResult findById(@RequestBody QueryPageBean page){
        log.info("分页查询检查项");
        PageResult pageResult = checkGroupService.findPage(page);
        return pageResult;
    }

    @PostMapping("/edit")
    public Result edit(@RequestBody CheckGroup checkGroup,   @RequestParam  List<Integer>checkitemIds ) {
        log.info("编辑检查组");
        checkGroupService.edit(checkGroup,checkitemIds);
        return new Result(true, MessageConstant.EDIT_CHECKGROUP_SUCCESS,null);
    }

    @GetMapping("findAll")
    public Result findAll(){
        log.info("查询所有检查组");
        List<CheckGroup>list=checkGroupService.findAll();
        return new Result(true, MessageConstant.QUERY_CHECKGROUP_SUCCESS,list);
    }

    @GetMapping("findById")
    public Result findById(@RequestParam Integer id){
        log.info("根据id查询检查项");
        CheckGroup checkItem=checkGroupService.findById(id);
        return new Result(true, MessageConstant.QUERY_CHECKITEM_SUCCESS,checkItem );
    }
}
